﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// News/Event Controller
    /// </summary>
    [Authorize]
    public class NewsEventController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly INewsEventRepository _newsEventRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public NewsEventController() : this(new NewsEventRepository())
        {
        }

        /// <summary>
        /// News/Event Controller Constructor
        /// </summary>
        /// <param name="newsEventRepository"></param>
        public NewsEventController(INewsEventRepository newsEventRepository)
        {
            _newsEventRepository = newsEventRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of News/Event
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<NewsEvent> newsEvent = !string.IsNullOrWhiteSpace(strQuery)
                                                  ? _newsEventRepository.FindNewsEventByName(strQuery).OrderBy(
                                                      d => d.CreatedDate)
                                                  : _newsEventRepository.FindAllNewsEvent().OrderBy(d => d.CreatedDate);
            PaginatedListCollection<NewsEvent> paginatedNewsEvent =
                newsEvent.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedNewsEvent);
        }

        /// <summary>
        /// Details of News/Event
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                NewsEvent newsEvent = _newsEventRepository.GetNewsEventById(id.Value);

                if (newsEvent == null)
                {
                    return new FileNotFoundResult {Message = "No Notification found for that id"};
                }

                return View(new NewsEventViewModel(newsEvent));
            }

            return new FileNotFoundResult {Message = "No Notification found due to invalid notification id"};
        }

        /// <summary>
        /// Create News/Event
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var newsEvent = new NewsEvent();
            return View(new NewsEventViewModel(newsEvent));
        }

        /// <summary>
        /// Create News/Event
        /// </summary>
        /// <param name="newsEvent"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(NewsEvent newsEvent)
        {
            if (ModelState.IsValid)
            {
                newsEvent.Title = newsEvent.Title;
                newsEvent.Description = HttpUtility.HtmlEncode(newsEvent.Description);
                newsEvent.UserCreated = HttpContext.User.Identity.Name;
                newsEvent.CreatedDate = DateTime.Now.ToSqlDateTime();
                newsEvent.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _newsEventRepository.Add(newsEvent);
                _newsEventRepository.Save();
                return RedirectToAction("Details", new {id = newsEvent.NewsEventId});
            }

            return View(new NewsEventViewModel(newsEvent));
        }

        /// <summary>
        /// Edit News/Event
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            NewsEvent newsEvent = _newsEventRepository.GetNewsEventById(id);
            return View(new NewsEventViewModel(newsEvent));
        }

        /// <summary>
        /// Edit News/Event
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            NewsEvent newsEvent = _newsEventRepository.GetNewsEventById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    newsEvent.UserModified = HttpContext.User.Identity.Name;
                    newsEvent.ModifiedDate = DateTime.Now;
                    UpdateModel(newsEvent, "NewsEvent");
                    _newsEventRepository.Save();
                    return RedirectToAction("Details", new {id = newsEvent.NewsEventId});
                }
                catch
                {
                    return View(new NewsEventViewModel(newsEvent));
                }
            }
            return View(new NewsEventViewModel(newsEvent));
        }

        /// <summary>
        /// Delete News/Event
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            NewsEvent newsEvent = _newsEventRepository.GetNewsEventById(id);
            return newsEvent == null ? View("NotFound") : View(new NewsEventViewModel(newsEvent));
        }

        /// <summary>
        /// Delete News/Event
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            NewsEvent newsEvent = _newsEventRepository.GetNewsEventById(id);

            if (newsEvent != null)
            {
                _newsEventRepository.Delete(newsEvent);
                _newsEventRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}